package com.zja.www.service.impl;

import com.zja.www.bean.Result;
import com.zja.www.dao.DoctorDao;
import com.zja.www.dao.impl.DoctorDaoImpl;
import com.zja.www.entity.Doctor;
import com.zja.www.service.DoctorService;

import java.util.Collections;
import java.util.List;
/**
 * @author ja
 * @create 2025/3/28
 * @description 医生service业务层接口实现类
 */

public class DoctorServiceImpl implements DoctorService {
    private DoctorDao doctorDao = new DoctorDaoImpl();

    public DoctorServiceImpl(DoctorDao doctorDao) {
        this.doctorDao = doctorDao;
    }

    @Override
    public boolean addDoctor(Doctor doctor) {
        if(doctor == null || doctor.getName() == null){
            return false;
        }
        if(doctorDao.getDoctorByName(doctor.getName()) != null){
            return false;
        }

        return doctorDao.insert(doctor);
    }

    @Override
    public Result login(String name, String password) {
        Doctor doctor = doctorDao.getDoctorByName(name);
        if (doctor == null) {
            return Result.fail("医生账号不存在");
        }

        if (!doctor.getPassword().equals(password)) {
            return Result.fail("密码错误");
        }

        return Result.success("登录成功", doctor);

    }

    @Override
    public Doctor getDoctorById(int id) {
        if(doctorDao.getDoctorById(id) != null){
            return doctorDao.getDoctorById(id);
        }
        return null;
    }

    @Override
    public List<Doctor> getAllDoctors() {
        if(doctorDao.getAllDoctors() != null){
            return doctorDao.getAllDoctors();
        }
        return null;
    }

    @Override
    public boolean updateDoctor(Doctor doctor) {
        if(doctor == null || getDoctorById(doctor.getId()) == null){
            return false;
        }
        return doctorDao.updateDoctor(doctor);
    }

    @Override
    public boolean deleteDoctor(int id) {
        if(doctorDao.getDoctorById(id) == null){
            return false;
        }
        return doctorDao.deleteDoctor(id);
    }

    @Override
    public List<Doctor> getDoctorsByDepartmentId(int departmentId) {

        if (departmentId <= 0) {
            return Collections.emptyList();
        }
        List<Doctor> doctors = doctorDao.getDoctorByDepartmentId(departmentId);


        if (doctors == null || doctors.isEmpty()) {
            System.out.println("没有该医生");
            return Collections.emptyList();
        }
        return doctors;
    }
}
